package com.cloud;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.RowSorter.SortKey;
import javax.swing.SortOrder;
import javax.swing.table.TableRowSorter;

import domain.Tag;

public class WordDiff {
	String selected;

	public WordDiff(String selected, List<Tag> tagsForType) {

		this.selected = selected;
		
		JFrame frame1 = new JFrame("Diff Table");
		JPanel Diff = new JPanel();
		Diff.setLayout((new GridLayout(1, 1)));
		System.out.println(selected + "Test2");

		String[] columnNames = { "Parent String", "Difference ",
				"Child String", };
		
		Object[][] data = new Object[tagsForType.size()][3];
		final JTable table = new JTable(data, columnNames);
		table.setPreferredScrollableViewportSize(new Dimension(100, 100));
		table.setFillsViewportHeight(true);
		javax.swing.table.TableModel model = table.getModel();
		String temp;
		int diff;
		Diff d = new Diff();

		System.out.println("Size::" + tagsForType.size());
		for (int i = 0; i < (tagsForType.size() - 1); i++)
		// for(int i=0;i<WORDS.length;i++)
		{
			Tag t = (Tag) tagsForType.get(i);
			System.out.println(t.getTag());
			temp = t.getTag();
			// temp=WORDS[i];
			diff = d.getLevenshteinDistance(selected, temp);
			for (int j = 0; j < 3; j++) {
				if (j == 0) {
					table.getModel().setValueAt(selected, i, j);

				} else if (j == 1) {
					if (diff < 10) {
						String dif = ((Integer) diff).toString();
						dif = "0" + dif;
						table.getModel().setValueAt(dif, i, j);
					} else {
						table.getModel().setValueAt(diff, i, j);
					}
				} else if (j == 2) {
					table.getModel().setValueAt(temp, i, j);
				}

			}

		}
		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.getViewport().setViewPosition(new Point(10, 0));
		Diff.add(scrollPane);
		frame1.add(Diff, BorderLayout.CENTER);
		frame1.setSize(800, 800);
		frame1.setVisible(true);
		frame1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		table.setEnabled(true);
		table.setAutoCreateRowSorter(true);
		TableRowSorter rowSorter = (TableRowSorter) table.getRowSorter();
		ArrayList<SortKey> keys = new ArrayList<SortKey>();
		SortKey sortKey = new SortKey(1, SortOrder.ASCENDING);
		keys.add(sortKey);
		rowSorter.setSortKeys(keys);
		rowSorter.sort();

	}
}